import MUtil from 'util/MUtil.jsx';
const mUtil = new MUtil();


class ProductService {
  /**
   * 获取商品列表
   * @param {页码} pageNum 
   */
  getProductList(listParam) {
    let url = '',
      data = {};
    if (listParam.listType === 'list') {
      url = '/manage/product/list.do';
      data.pageNum = listParam.pageNum;
    } else if (listParam.listType === 'search') {
      url = '/manage/product/search.do'
      data.pageNum = listParam.pageNum;
      data[listParam.searchType] = listParam.keyword;
    }
    return mUtil.request({ type: 'post', url, data });

  }
  /**
   * 设置商品上下架
   */
  setProductStatus(productInfo) {
    return mUtil.request({
      type: 'post',
      url: '/manage/product/set_sale_status.do',
      data: productInfo
    });
  }
  /**
   * 品类
   */
  getCategoryList(parentCategoryId) {
    return mUtil.request({
      type: 'post',
      url: '/manage/category/get_category.do',
      data: {
        categoryId: parentCategoryId || 0
      }
    });
  }
  /**
   * 表单验证
   * @param {添加商品的表单信息} product 
   */
  checkProduct(product) {
    let result = {
      status: true,
      msg: '验证通过'
    };

    if (typeof product.name !== 'string' || product.name.length <= 0) {
      return {
        status: false,
        msg: '商品名称不能为空'
      }
    }

    if (typeof product.subtitle !== 'string' || product.subtitle.length <= 0) {
      return {
        status: false,
        msg: '商品描述不能为空'
      }
    }
    if (typeof product.categoryId !== 'number' || product.categoryId <= 0) {
      return {
        status: false,
        msg: '请选择商品品类'
      }
    }
    if (typeof product.price !== 'number' || product.price <= 0) {
      return {
        status: false,
        msg: '请输入正确的商品价格'
      }
    }

    if (typeof product.stock !== 'number' || product.stock < 0) {
      return {
        status: false,
        msg: '库存为数字且不能小于零'
      }
    }
    return result;
  }
  /**
   * 表单提交
   * @param {添加商品的表单信息} product 
   */
  saveProduct(product) {
    return mUtil.request({
      type: 'post',
      url: '/manage/product/save.do',
      data: product,
    });
  }
  /**
   * 获取商品详情
   * @param {商品Id} productId 默认为0
   */
  getProduct(productId = 0) {
    return mUtil.request({
      type: 'get',
      url: '/manage/product/detail.do',
      data: { productId }
    });
  }
  /**
   * 更新品类名称
   */
  updateCategoryName(category) {
    return mUtil.request({
      type: 'post',
      url: '/manage/category/set_category_name.do',
      data: category,
    });
  }

  /**
   * 添加品类信息
   * @param {品类信息} category 
   */
  saveCategory(category) {
    return mUtil.request({
      type: 'post',
      url: '/manage/category/add_category.do',
      data: category,
    });
  }
}

export default ProductService;